//
//  PrivateOrgDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/6/25.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "PrivateOrgDetailViewController.h"
@interface PrivateOrgDetailViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseNameField;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseTypeField;
@property(nonatomic,strong)NSArray <PropertyDictItemModel *>*enterpriseTypes;
@property(nonatomic,strong)PropertyDictItemModel *enterpriseTypeModel;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseAddressField;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseMobileField;
@property (weak, nonatomic) IBOutlet UITextField *enterpriseTelField;

@property (weak, nonatomic) IBOutlet SelectedButton *isDangerousEntBtn;

@property (weak, nonatomic) IBOutlet SelectedButton *notDangerousEntBtn;

@property (weak, nonatomic) IBOutlet UITextField *manipulatorNameField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorMobileField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorTelField;

@property (weak, nonatomic) IBOutlet UITextField *cardTypeField;
@property(nonatomic,strong)NSArray <PropertyDictItemModel *>*cardTypes;
@property(nonatomic,strong)PropertyDictItemModel *cardModel;


@property (weak, nonatomic) IBOutlet UITextField *manipulatorCardNumField;
@property (weak, nonatomic) IBOutlet UITextField *protectorNameField;
@property (weak, nonatomic) IBOutlet UITextField *protectorMobileField;
@property (weak, nonatomic) IBOutlet UITextField *protectorTelField;

@property (weak, nonatomic) IBOutlet UITextField *creditCodeField;

@property (weak, nonatomic) IBOutlet UITextField *concernStateField;

@property(nonatomic,strong)NSArray <PropertyDictItemModel *> *concernedArray;
@property(nonatomic,strong)PropertyDictItemModel *concernModel;

@property (weak, nonatomic) IBOutlet UITextField *problemTypeField;
@property(nonatomic,strong)NSArray <PropertyDictItemModel *>*problems;
@property(nonatomic,strong)PropertyDictItemModel *problemModel;

@end

@implementation PrivateOrgDetailViewController

- (IBAction)havingFemaleOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingFemaleOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)notHavingFemaleOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingFemaleOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingYoungthOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingYoungthOrgBtn.selected = NO;
    [self.tableView reloadData];
}


- (IBAction)notHavingYoungthOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingYoungthOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingWorkingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingWorkingOrgBtn.selected = NO;
    [self.tableView reloadData];
}
- (IBAction)notHavingWorkingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingWorkingOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingOrgBtn.selected = NO;
    [self.tableView reloadData];
}
- (IBAction)notHavingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)canBiuildDAction:(UIButton *)sender {
    sender.selected = YES;
    self.cantBuildDBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)cantBuildDAction:(UIButton *)sender {
    sender.selected = YES;
    self.canBuildDBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)isDangerousEntAction:(UIButton *)sender {
    sender.selected = YES;
    self.notDangerousEntBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)notDangerousEntAction:(UIButton *)sender {
    sender.selected = YES;
    self.isDangerousEntBtn.selected = NO;
    [self.tableView reloadData];
}

-(instancetype)init{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Organization" bundle:[NSBundle mainBundle]];
    return [story instantiateViewControllerWithIdentifier:@"PrivateOrgDetailViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)uploadFiles:(NSArray <NSData *>*)files successBlock:(void(^)(NSArray <FileItemModel *>*))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:files fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:[FileItemModel class]];
        if (model.success) {
            if (suc) {
                suc(model.list);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)selectImage{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        self.headImageView.image = photos[0];
    };
    [self presentVC:picker];
}

-(void)chooseEnterpriseType{
    [self commonAlertWithContent:@"社会组织类型" choosenBlock:^(PropertyDictItemModel *model) {
        self.enterpriseTypeModel = model;
        self.enterpriseTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.enterpriseTypes = list;
    }];
}

-(void)chooseCardType{
    
    [self commonAlertWithContent:@"证件代码" choosenBlock:^(PropertyDictItemModel *model) {
        self.cardModel = model;
        self.cardTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.cardTypes = list;
    }];
    
}

-(void)chooseConcern{
    [self commonAlertWithContent:@"社会关注程度" choosenBlock:^(PropertyDictItemModel *model) {
        self.concernModel = model;
        self.concernStateField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.concernedArray = list;
    }];
}

-(void)chooseProblemType{
    [self commonAlertWithContent:@"安全隐患类型" choosenBlock:^(PropertyDictItemModel *model) {
        self.problemModel = model;
        self.problemTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.problems = list;
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //19 20 21 22
    switch (indexPath.row) {
        case 19:{
            UITableViewCell *cell = self.dangyuanCell;
            cell.hidden = !self.havingOrgBtn.selected;
            return cell;
        }
            break;
        case 20:{
            UITableViewCell *cell = self.gonghuiCell;
            cell.hidden = !self.havingWorkingOrgBtn.selected;
            return cell;
        }
            break;
        case 21:{
            UITableViewCell *cell = self.femaleCell;
            cell.hidden = !self.havingFemaleOrgBtn.selected;
            return cell;
        }
            break;
        case 22:{
            UITableViewCell *cell = self.tuanCell;
            cell.hidden = !self.havingYoungthOrgBtn.selected;
            return cell;
        }
        default:
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 19:
            return self.havingOrgBtn.selected ? 44 : 0.0;
            break;
        case 20:
            return self.havingWorkingOrgBtn.selected ? 44 : 0.0;
            break;
            
        case 21:
            return self.havingFemaleOrgBtn.selected ? 44 : 0.0;
            break;
            
        case 22:
            return self.havingYoungthOrgBtn.selected ? 44 : 0.0;
            break;
        default:
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            break;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self selectImage];
            break;
        case 2:
            [self chooseEnterpriseType];
            break;
        case 8:
            [self chooseCardType];
            break;
        case 22:
            [self chooseConcern];
            break;
        case 23:
            [self chooseProblemType];
            break;
        default:
            break;
    }
}

#pragma mark - 获取详情
-(NSString *)detailPath{
    return @"/mobile/comprehensiveGovernance/newEconomicOrganizations/getNewEconomicOrganizationsById.action";
}

-(NSDictionary *)detailParam{
    return @{@"id":self.ids};
}

-(void(^)(NSDictionary *))configView{
    void(^config)(NSDictionary *) = ^(NSDictionary *source){
        NSDictionary *data = source[@"data"];
        NSDictionary *concern = data[@"attentionExtent"];
        self.concernModel = [PropertyDictItemModel new];
        self.concernModel.ids = concern[@"id"];
        self.concernStateField.text = concern[@"displayName"];
        self.manipulatorCardNumField.text = data[@"cardNo"];
        self.manipulatorNameField.text = data[@"chief"];
        self.cardModel = [PropertyDictItemModel new];
        self.cardModel.ids = data[@"chiefCardType"][@"id"];
        self.cardTypeField.text = data[@"chiefCardType"][@"displayName"];
        self.protectorNameField.text = data[@"chiefSafe"];
        self.problemModel = [PropertyDictItemModel new];
        self.problemModel.ids = data[@"dangerType"][@"id"];
        self.problemTypeField.text = data[@"dangerType"][@"displayName"];
        self.employeeCountField.text = [NSString stringWithFormat:@"%li",[data[@"employeeNumber"] integerValue]];
        self.enterpriseMobileField.text = data[@"enterpriseMobile"];
        self.enterpriseTelField.text = data[@"enterpriseTelephone"];
        self.isDangerousEntBtn.selected = [data[@"isDangerousEnterprise"] boolValue];
        self.notDangerousEntBtn.selected = !self.isDangerousEntBtn.selected;
        self.canBuildDBtn.selected = [data[@"isHaveOrganizationCondition"] boolValue];
        self.cantBuildDBtn.selected = !self.canBuildDBtn.selected;
        self.havingYoungthOrgBtn.selected = [data[@"isHaveGroup"] boolValue];
        self.notHavingYoungthOrgBtn.selected = !self.havingYoungthOrgBtn.selected;
        self.havingOrgBtn.selected = [data[@"isHaveOrganization"] boolValue];
        self.notHavingOrgBtn.selected = !self.havingOrgBtn.selected;
        self.havingWorkingOrgBtn.selected = [data[@"isHaveGuild"] boolValue];
        self.notHavingWorkingOrgBtn.selected = !self.havingWorkingOrgBtn.selected;
        self.havingFemaleOrgBtn.selected = [data[@"isHaveWomenFederation"] boolValue];
        self.notHavingFemaleOrgBtn.selected = !self.havingFemaleOrgBtn;
        self.creditCodeField.text = data[@"licenseNumber"];
        self.enterpriseMobileField.text = data[@"mobileNumber"];
        self.protectorMobileField.text = data[@"mobileNumberSafe"];
        self.enterpriseNameField.text = data[@"name"];
        self.enterpriseTypeModel = [PropertyDictItemModel new];
        self.enterpriseTypeModel.ids = data[@"style"][@"id"];
        self.enterpriseTypeField.text = data[@"style"][@"displayName"];
        self.dangyuanCountField.text = [NSString stringWithFormat:@"%li",[data[@"partyMemberNumber"] integerValue]];
        self.enterpriseAddressField.text = data[@"residence"];
        self.manipulatorTelField.text = data[@"telephone"];
        self.manipulatorMobileField.text = data[@"mobileNumber"];
        self.protectorTelField.text = data[@"telephoneSafe"];
        self.dangMemberCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfGroup"] integerValue]];
        self.gonghuiMemberCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfGuild"] integerValue]];
        self.femaleCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfWomenFederation"] integerValue]];
        NSString *imgUrl = [NSString stringWithFormat:@"%@/%@",ServerPath,data[@"imgUrl"]];
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:self.headImageView.image];
        [self.tableView reloadData];
    };
    return config;
}

#pragma 更新记录
-(NSString *)updatePath{
    return @"/mobile/comprehensiveGovernance/newEconomicOrganizations/updateNewEconomicOrganizations.action";
}

-(NSDictionary *)updateParameter{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.ids.length > 0) {
        param[@"newEconomicOrganization.id"] = self.ids;
    }
    NSString *orgId = [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
    if (orgId.length > 0) {
        param[@"orgId"] = orgId;
    }
    if (self.enterpriseAddressField.text.length == 0) {
        [self alertSome:@"必须输入企业地址!"];
        return nil;
    }
    param[@"newEconomicOrganization.residence"] = self.enterpriseAddressField.text;
    if (self.enterpriseNameField.text.length == 0) {
        [self alertSome:@"必须输入企业名称"];
        return nil;
    }
    param[@"newEconomicOrganization.name"] = self.enterpriseNameField.text;
    if (self.enterpriseTypeModel.ids.length == 0) {
        [self alertSome:@"必须选择企业类型"];
        return nil;
    }
    param[@"newEconomicOrganization.style.id"] = self.enterpriseTypeModel.ids;
    if (self.enterpriseTelField.text.length == 0 || self.enterpriseMobileField.text.length == 0) {
        [self alertSome:@"必须输入企业联系方式"];
        return nil;
    }
    param[@"newEconomicOrganization.enterpriseMobile"] = self.enterpriseMobileField.text;
    param[@"newEconomicOrganization.enterpriseTelephone"] = self.enterpriseTelField.text;
    if (self.isDangerousEntBtn.selected) {
        param[@"newEconomicOrganization.isDangerousEnterprise"] = @"true";
    }else{
        param[@"newEconomicOrganization.isDangerousEnterprise"] = @"false";
    }
    if (self.cardModel.ids.length == 0) {
        [self alertSome:@"请选择法定代表人证件类型!"];
        return nil;
    }
    param[@"newEconomicOrganization.chiefCardType.id"] = self.cardModel.ids;
    if (self.manipulatorCardNumField.text.length == 0) {
        [self alertSome:@"请输入法定代表人证件号码!"];
        return nil;
    }
    param[@"newEconomicOrganization.cardNo"] = self.manipulatorCardNumField.text;
    if (self.manipulatorNameField.text.length == 0) {
        [self alertSome:@"请输入法定代表人姓名!"];
        return nil;
    }
    param[@"newEconomicOrganization.chief"] = self.manipulatorNameField.text;
    if (self.protectorNameField.text.length == 0) {
        [self alertSome:@"请输入治保负责人姓名!"];
        return nil;
    }
    param[@"newEconomicOrganization.chiefSafe"] = self.protectorNameField.text;
    if (self.manipulatorTelField.text.length == 0) {
        [self alertSome:@"请输入法定负责人座机!"];
        return nil;
    }
    param[@"newEconomicOrganization.telephone"] = self.manipulatorTelField.text;
    if (self.manipulatorMobileField.text.length == 0) {
        [self alertSome:@"请输入法定负责人手机号!"];
        return nil;
    }
    param[@"newEconomicOrganization.mobileNumber"] = self.manipulatorMobileField.text;
    if (self.protectorMobileField.text.length == 0) {
        [self alertSome:@"请输入治保负责人手机号!"];
        return nil;
    }
    param[@"newEconomicOrganization.mobileNumberSafe"] = self.protectorMobileField.text;
    if (self.protectorTelField.text.length == 0) {
        [self alertSome:@"请输入治保负责人座机!"];
        return nil;
    }
    param[@"newEconomicOrganization.telephoneSafe"] = self.protectorTelField.text;
    if (self.creditCodeField.text.length == 0) {
        [self alertSome:@"请输入工商执照注册号码!"];
        return nil;
    }
    param[@"newEconomicOrganization.licenseNumber"] = self.creditCodeField.text;
    if (self.canBuildDBtn.selected) {
        param[@"newEconomicOrganization.isHaveOrganizationCondition"] = @"true";
    }else{
        param[@"newEconomicOrganization.isHaveOrganizationCondition"] = @"false";
    }
    if (self.havingOrgBtn.selected) {
        param[@"newEconomicOrganization.isHaveOrganization"] = @"true";
        if (self.dangyuanCountField.text.length == 0) {
            [self alertSome:@"请输入党员人数!"];
            return nil;
        }
        param[@"newEconomicOrganization.partyMemberNumber"] = self.dangyuanCountField.text;
    }else{
        param[@"newEconomicOrganization.isHaveOrganization"] = @"false";
    }
    if (self.havingWorkingOrgBtn.selected) {
        param[@"newEconomicOrganization.isHaveGuild"] = @"true";
        if (self.gonghuiMemberCountField.text.length == 0) {
            [self alertSome:@"请输入工会会员数量!"];
            return nil;
        }
        param[@"newEconomicOrganization.theMemeberOfGuild"] = self.gonghuiMemberCountField.text;
    }else{
        param[@"newEconomicOrganization.isHaveGuild"] = @"false";
    }
    if (self.havingYoungthOrgBtn.selected) {
        param[@"newEconomicOrganization.isHaveGroup"] = @"true";
        if (self.dangMemberCountField.text.length == 0) {
            [self alertSome:@"请输入共青团员数量!"];
            return nil;
        }
        param[@"newEconomicOrganization.theMemeberOfGroup"] = self.dangMemberCountField.text;
    }else{
        param[@"newEconomicOrganization.isHaveGroup"] = @"false";
    }
    if (self.havingFemaleOrgBtn.selected) {
        param[@"newEconomicOrganization.isHaveWomenFederation"] = @"true";
        if (self.femaleCountField.text.length == 0) {
            [self alertSome:@"请输入妇女数量!"];
            return nil;
        }
        param[@"newEconomicOrganization.theMemeberOfWomenFederation"] = self.femaleCountField.text;
    }else{
        param[@"newEconomicOrganization.isHaveWomenFederation"] = @"false";
    }
    if (self.employeeCountField.text.length == 0) {
        [self alertSome:@"请输入企业员工数!"];
        return nil;
    }
    param[@"newEconomicOrganization.employeeNumber"] = self.employeeCountField.text;
    
    if (self.concernModel.ids.length == 0) {
        [self alertSome:@"请选择关注程度!"];
        return nil;
    }
    param[@"newEconomicOrganization.attentionExtent.id"] = self.concernModel.ids;
    if (self.filePath.length > 0) {
        param[@"file.path"] = self.filePath;
    }
    if (self.problemModel.ids.length == 0) {
        [self alertSome:@"请选择安全隐患类型!"];
        return nil;
    }
    param[@"newEconomicOrganization.dangerType.id"] = self.problemModel.ids;
    return param;
}

-(void)updateRecord{
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            if (files.count > 0) {
                FileItemModel *model = files[0];
                self.filePath = model.path;
                [super updateRecord];
            }
        }];
    }else{
        [super updateRecord];
    }
}

//新增记录
-(NSString *)addPath{
    return @"/mobile/comprehensiveGovernance/newEconomicOrganizations/addNewEconomicOrganizations.action";
}

-(NSDictionary *)addedParameter{
    return [self updateParameter];
}

-(void)addRecord{
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            if (files.count > 0) {
                FileItemModel *model = files[0];
                self.filePath = model.path;
                [super addRecord];
            }
        }];
    }else{
        [super addRecord];
    }
}

//删除记录
-(NSString *)delPath{
    return @"/mobile/comprehensiveGovernance/newEconomicOrganizations/deleteNewEconomicOrganizationsById.action";
}

-(NSDictionary *)delParameter{
    return @{@"id":self.ids};
}

//操作完成后发送的通知
-(NSString *)refresh_not_name{
    return @"refresh_private_org";
}





@end
